﻿
using System.Net;
using System.Net.Mail;
using Foothill.Common.Logging;
using System;

namespace Foothill.Service.Email
{
    public class EmailSender : IEmailSender
    {
        public void SendEmail(MailMessage mailMessage)
        {
            using (SmtpClient smtpClient = ConstructSmtpClient())
            {
                try
                {
                    smtpClient.Send(mailMessage);
                }
                catch (SmtpFailedRecipientsException err)
                {
                    Log.Error(FoothillModule.Service, "SmtpFailedRecipientsException thrown by EmailSender", err);
                    throw;
                }
                catch (SmtpException err)
                {
                    Log.Error(FoothillModule.Service, "SmtpException thrown by EmailSender", err);
                    throw;
                }
                catch (Exception err)
                {
                    Log.Error(FoothillModule.Service, string.Format("{0} thrown by EmailSender", err.GetType()), err);
                    throw;
                }
            }
        }

        private SmtpClient ConstructSmtpClient()
        {
            // construct and initialise the smtp client by using the settings from <mailSettings> section in the configuration file.
            SmtpClient smtpClient = new SmtpClient();
            return smtpClient;
        }
    }
}
